package com.resolution.dietaryassessment.view.mydietary.fragment.resultitem;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.bin.david.form.core.SmartTable;
import com.bin.david.form.data.column.Column;
import com.bin.david.form.data.format.bg.BaseBackgroundFormat;
import com.bin.david.form.data.style.FontStyle;
import com.bin.david.form.data.style.LineStyle;
import com.bin.david.form.data.table.TableData;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.resolution.dietaryassessment.databeans.CommonConfig;
import com.resolution.dietaryassessment.R;
import com.resolution.dietaryassessment.databeans.Statistical;
import com.resolution.dietaryassessment.databeans.UserRealData;
import com.resolution.dietaryassessment.view.mydietary.surface.MyIntakeAssess;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;




/**
 * @author By Resolusion
 * @Title: 营养素摄入评价
 * @Date: 2020/5/7
 * @Time: 16:29
 * @email Res01ution@foxmail.com
 */
public class IntakeAssessFrag extends Fragment {
    private View view;

    private List<BarEntry> barEntryList = new ArrayList<>();
    private List<BarEntry> entriesGroup2 = new ArrayList<>();
    private List<String> xAxisNameList = new ArrayList<>();
    private List<MyIntakeAssess> myIntakeAssessList = new ArrayList<>();

    public static IntakeAssessFrag newInstance() {
        Bundle args = new Bundle();
        IntakeAssessFrag fragment = new IntakeAssessFrag();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_intake_assess, container, false);
        initView();
        return view;
    }

    private void initView() {
        Statistical statistical = CommonConfig.getInstance().getStatistical();
        UserRealData userRealData = CommonConfig.getInstance().getUserRealData();
        myIntakeAssessList.add(new MyIntakeAssess("能量(Kcal)",
                statistical.getUserHeat(),userRealData.getUserHeat()));
        myIntakeAssessList.add(new MyIntakeAssess("蛋白质(克)",
                statistical.getUserProtein(),userRealData.getUserProtein()));
        myIntakeAssessList.add(new MyIntakeAssess("脂肪(克)",
                statistical.getUserFat(),userRealData.getUserFat()));
        myIntakeAssessList.add(new MyIntakeAssess("碳水化合物(克)",
                statistical.getUserCBHR(),userRealData.getUserCBHR()));

        //普通列
        Column<String> column1 = new Column<>("营养表", "nutrition");
        Column<Integer> column2 = new Column<>("推荐量", "rQuantity");
        Column<Integer> column3 = new Column<>("我的摄入量", "myQuantity");
        Column<String> column4 = new Column<>("评价", "evaluate");

        final TableData<MyIntakeAssess> tableData = new TableData<>("我的营养素摄入评价",myIntakeAssessList,column1,column2,column3,column4);
        //设置数据
        SmartTable<MyIntakeAssess> table = view.findViewById(R.id.tab);
        table.getConfig().setShowXSequence(false);
        table.getConfig().setShowYSequence(false);
        //表格线style
        table.getConfig().setContentGridStyle(new LineStyle(1,getResources().getColor(R.color.colorDashLine)));
        //列标题表格线
        table.getConfig().setColumnTitleGridStyle(new LineStyle(1,getResources().getColor(R.color.colorDashLine)));
        table.getConfig().setColumnTitleStyle(new FontStyle(getContext(),14,getResources().getColor(R.color.colorMidBlack)));
        table.getConfig().setTableTitleStyle(new FontStyle(getContext(),20,getResources().getColor(R.color.colorMidBlack)));
        table.getConfig().setContentBackground(new BaseBackgroundFormat(getResources().getColor(R.color.colorWeakBlue)));
        table.getConfig().setColumnTitleBackground(new BaseBackgroundFormat(getResources().getColor(R.color.colorWeakOrange)));
        table.setTableData(tableData);
        initBarChart();
    }

    private void initBarChart() {

        Collections.addAll(xAxisNameList,"能量","蛋白质","脂肪","碳水化合物");

        BarChart barChart = view.findViewById(R.id.intake_chart);

        for (int i = 0;i < xAxisNameList.size();i++){
            barEntryList.add(new BarEntry(i, 100f));
            entriesGroup2.add(new BarEntry(i,(myIntakeAssessList.get(i).getMyQuantity() / myIntakeAssessList.get(i).getrQuantity())* 100));
        }


        barChart.setNoDataText("正在初始化...");
        //清除点击
        barChart.setTouchEnabled(false);
        Description description = new Description();
        description.setTextSize(10);
        description.setText("比例%");
        description.setTextColor(getResources().getColor(R.color.colorMidBlack));
        barChart.setDescription(description);

        XAxis xAxis = barChart.getXAxis();
        xAxis.setLabelCount(4,false);
        xAxis.setDrawGridLines(false);
        xAxis.setDrawAxisLine(false);
        xAxis.setCenterAxisLabels(true);
        xAxis.setAxisMinimum(0);
        xAxis.setAxisMaximum(4);
        xAxis.setSpaceMin(1);
        xAxis.setSpaceMax(1);

        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);

        xAxis.setValueFormatter(XAxisCustom(xAxisNameList));

        YAxis leftAxis = barChart.getAxisLeft();
        leftAxis.setDrawGridLines(false);
        leftAxis.setLabelCount(4, false);
        //设置Y轴最小坐标和最大坐标
        leftAxis.setAxisMinimum(0f);

        //清除右边Y轴
        barChart.getAxisRight().setEnabled(false);


        BarDataSet dataSet = new BarDataSet(barEntryList, "推荐量(%)");
        dataSet.setColor(getResources().getColor(R.color.colorFraction));
        BarDataSet set2 = new BarDataSet(entriesGroup2, "摄入量(%)");
        set2.setColor(getResources().getColor(R.color.colorWeakOrange));


        float groupSpace = 0.56f;
        float barSpace = 0.02f; // x4 DataSet
        float barWidth = 0.2f;

        BarData data = new BarData(dataSet, set2);
        data.setBarWidth(barWidth);
        barChart.setData(data);
        barChart.groupBars(0f, groupSpace, barSpace);
        barChart.invalidate();
    }

    /**
     * 自定义X轴
     */
    private IAxisValueFormatter XAxisCustom(List<String> list) {
        return new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                if (value >= 0 && value < list.size()) {
                    return list.get((int) value);
                } else {
                    return String.valueOf((int) value);
                }
            }
            @Override
            public int getDecimalDigits() {
                return 0;
            }
        };
    }

}
